/**
 * com.dingjian.permission.controller.PermissionListController.java
 */
package com.dingjian.permission.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dingjian.base.CoreEntity;
import com.dingjian.base.service.BaseService;
import com.dingjian.base.util.StringUtils;
import com.dingjian.framework.base.controller.ListController;
import com.dingjian.permission.model.PermissionItem;
import com.dingjian.permission.model.PermissionGroup;
import com.dingjian.permission.service.PermissionGroupService;
import com.dingjian.permission.service.PermissionItemService;

/**
 * @author FengMy
 * 
 * @since 2012-9-18
 */
@Controller(value="permissionListController")
@RequestMapping(value="permission/permissionItem/*")
public class PermissionItemListController extends ListController {
	@Autowired
	private PermissionItemService permissionService;
	@Autowired
	private PermissionGroupService permissionGroupService;
	@Override
	protected CoreEntity createNewEntity() {
		String groupid = getString("group");
		PermissionItem permission = new PermissionItem(); 
		if(!StringUtils.isEmpty(groupid)){
			PermissionGroup group = permissionGroupService.getEntityById(groupid);
			permission.setGroup(group);
		}
		return permission;
	}

	@Override
	protected String getListView() {
		return "permission/permissionItemList";
	}

	@Override
	protected String getEditView() {
		return "permission/permissionItemEdit";
	}

	@Override
	protected String getListMapper() {
		return "com.dingjian.permission.dao.PermissionItemDao.select";
	}

	@Override
	protected BaseService<PermissionItem> getService() {
		return permissionService;
	}

	/**
	 * 多选数据选择器
	 * @param model
	 * @return
	 */
	@RequestMapping(value="permissionDataPicker")
	public String permissionDataPicker(ModelMap model){
		Map<String,String> param = getParamMap();
		Set<String> keys = param.keySet();
		for(String s : keys){
			model.put(s, param.get(s));
		}
		return "permission/permissionDataPicker";
	}
	
	/**
	 * 树形多选数据选择器(一次全加载)
	 * @return
	 */
	@RequestMapping(value="permissionTreeDataPicker")
	public String permissionTreeDataPicker(){
		return "permission/permissionTreeDataPicker";
	}
	
	@RequestMapping(value="permissionAll")
	public void permissionAll(HttpServletResponse response){
		@SuppressWarnings("rawtypes")
		List<Map> allData = queryExecutor.execQuery("com.dingjian.permission.dao.PermissionItemDao.getPermissionItemTree", null, Map.class);
		outPrint(response, JSONArray.fromObject(allData));
	}
	
	@RequestMapping(value="loadPermissions")
	public synchronized void loadPermissions(){
		List<PermissionItem> permissions = queryExecutor.execQuery(getListMapper(), new HashMap<String,Object>(), PermissionItem.class);
		Set<String> permissionMethods = new HashSet<String>();
		for(PermissionItem permission : permissions){
			permissionMethods.add(permission.getMethod());
		}
		getServletContext().setAttribute("WHOLE-PERMISSIONMETHODS", permissionMethods);
	}
}
